const app = getApp()
import BaseModel from '../../utils/common.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    earlyMes:"",//早会信息
    teacherValueId:"",//选择的讲师id
    teacherValueName:"选择讲师",//选择的讲师名字
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
    })

  },
  onShow: function () {
    let pages = getCurrentPages();
    let currPage = pages[pages.length-1];
    this.setData({
      teacherValueId:currPage.data.staffIdAll,
      teacherValueName:currPage.data.staffNameAll,
    })
    this.getEarlyOrderData(); //获取早会信息
  },
  //提交信息
  getEarlyOrderModel:new BaseModel({
    url: "detailsMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getEarlyOrderData(){
    this.getEarlyOrderModel
        .fetch({
          morningId:this.data.id
        })
        .then(res => {
          //成功
          this.setData({
            earlyMes:res.data,
            teacherValueName:res.data.tutor_name
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (options) {

  },
  /*拒绝早会*/
  refuse:function(){
    var that = this;
    wx.showModal({
      title: '确定要拒绝此次早会上报申请吗',
      content: "",
      success(res) {
        if (res.confirm) {
          that.getRefuseData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //拒绝早会提交信息
  getRefuseModel:new BaseModel({
    url: "refuseMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getRefuseData(){
    this.getRefuseModel
        .fetch({
          morningId :this.data.id
        })
        .then(res => {
          //成功
          wx.redirectTo({
            url: './early_approval'
          })
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*同意早会*/
  agree:function(){
    var that = this;
    /*指定讲师*/
    if(this.data.teacherValueId==""||this.data.teacherValueId==undefined){
      wx.showToast({
        title: '讲师未指定',
        icon: "none"
      })
      return false;
    }
    wx.showModal({
      title: '确定要同意此次早会上报申请吗',
      content: "",
      success(res) {
        if (res.confirm) {
          that.getAgreeData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //同意早会提交信息
  getAgreeModel:new BaseModel({
    url: "byMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getAgreeData(){
    this.getAgreeModel
        .fetch({
          morningId :this.data.id, //早会id
          tutor_id :this.data.teacherValueId  //指定讲师id
        })
        .then(res => {
          //成功
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          //失败 2018-12-20 00:00:00
        })
  },
  /*删除早会*/
  delete:function(){
    var that = this;
    wx.showModal({
      title: '确定要删除此次早会上报申请吗',
      content: "",
      success(res) {
        if (res.confirm) {
          that.getDeleteData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除早会提交信息
  getDeleteDataModel:new BaseModel({
    url: "deleteMorning",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getDeleteData(){
    this.getDeleteDataModel
        .fetch({
          morningId :this.data.id, //早会id
        })
        .then(res => {
          //成功
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            success(res){
              wx.navigateBack({
                delta:1
              })
            }
          })

        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /*修改早会信息*/
  edit:function(){
    wx.navigateTo({
      url:"early_add?id="+this.data.id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getEarlyOrderData();
    wx.stopPullDownRefresh(); //停止下拉
  },

})